package net.wnb.mineclearance.ui;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import net.wnb.mineclearance.model.Game;

import wnb.util.ui.AnimationPane;

public class GameView extends AnimationPane implements MouseListener, MouseMotionListener {
	private GameAnimation animation;

	public GameView(GameAnimation animation, int delay, boolean loop) {
		super(delay, loop);
		
		this.animation = animation;
		super.setAnimation(animation);
		addMouseListener(this);
		addMouseMotionListener(this);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private int getRowIndex(Point point) {
		Game game = animation.getGame();
		int height = getHeight() / game.getRowCount();
		return point.y / height;
	}

	private int getColIndex(Point point) {
		Game game = animation.getGame();
		int width = getWidth() / game.getColCount();
		return point.x / width;
	}

	@Override
	public void mouseClicked(MouseEvent event) {		
		Game game = animation.getGame();
		if(game.getState() != Game.STATE_PROCESSING)
			return;
		
		Point point = event.getPoint();
		int rowIndex = getRowIndex(point);
		int colIndex = getColIndex(point);
		game.open(rowIndex, colIndex);
	}

	@Override
	public void mouseEntered(MouseEvent event) {
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		animation.setMouseGrid(-1, -1);
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(MouseEvent event) {
		Point point = event.getPoint();
		int rowIndex = getRowIndex(point);
		int colIndex = getColIndex(point);
		animation.setMouseGrid(rowIndex, colIndex);
	}

}
